local log_server_url = os.getenv("log_server_url");
local resp_header = ngx.resp.get_headers();
--ngx.log(ngx.INFO,"req header===========", ngx.ctx.req_header["sad"]);
--ngx.log(ngx.INFO,"req body===========", ngx.ctx.req_body);
				
--ngx.log(ngx.INFO,"ngx.ctx.response_header===========", resp_header["Content-Type"]);
--ngx.log(ngx.INFO,"ngx.ctx.response_body===========", ngx.ctx.resp_body);

function table.kIn(tbl, key)
    if tbl == nil then
        return false
    end
    for k, v in pairs(tbl) do
        if string.lower(k) == string.lower(key) then
            return true
        end
    end
    return false
end

local res_content_type = "";
if table.kIn(resp_header, "Content-Type") then
    res_content_type = resp_header["Content-Type"];
elseif table.kIn(resp_header, "content-type") then
    res_content_type = resp_header["content-type"];
end

local user_id = "";
if table.kIn(ngx.ctx.req_header, "accessToken") then
    user_id = ngx.ctx.req_header["accessToken"];
else
    user_id = "";
end
local request_time = os.date("%Y-%m-%d %H:%M:%S", ngx.req.start_time());
local url = ngx.var.request_uri;
local request_method = ngx.var.request_method;
local request_param = ngx.ctx.req_body;
local reponse_code = ngx.status;
local response_data = ngx.ctx.resp_body;
if (response_data == nil) then
    response_data = "";
end
local remote_addr  = ngx.var.remote_addr;
local source_ip = ngx.var.host;
local upstream = require "ngx.upstream";
local up_url = upstream.current_upstream_name();
if (up_url == nil or up_url == '') then
    up_url = "localhost:80";
end
local destination_ip = string.sub(up_url,1,string.find(up_url,":")-1);
local port = string.sub(up_url,string.find(up_url,":")+1);
				
local function push_data(premature,user_id,request_time,url,request_method,request_param,reponse_code,response_data,remote_addr,source_ip,destination_ip,port)
	local http = require ("http");
	local httpc = http.new();
	local cjson = require("cjson");
	local obj = {
		userId = user_id,
		requestTime = request_time,
		url = url,
		requestMethod = request_method,
		requestParam = request_param,
		httpStatusCode = reponse_code,
		responseMessage = string.sub(response_data, 1, 3000),
		responseData = response_data,
		clientIp = remote_addr,
		sourceIp = source_ip,
		destinationIp = destination_ip,
		port = port,
	};
	-- 需要post的参数
	local str = cjson.encode(obj);
	-- 请求地址
	local url = log_server_url;
	local res, err = httpc:request_uri(url, {
		method = "POST",
		body = str,
		headers = {
			["Content-Type"] = "application/json",
			["Content-Length"] = #str,
			["Accept"] = "*/*",
		}
	});
	if (res.status ~= 200) then
		ngx.log(ngx.ERR,"log server response===========", res.status);
	end
end

if res_content_type=="application/json" or res_content_type=="text/html" or res_content_type=="" or res_content_type==nil then
	local ok, err = ngx.timer.at(0, push_data, user_id,request_time,url,request_method,request_param,reponse_code,response_data,remote_addr,source_ip,destination_ip,port);
				
	if not ok then
		ngx.log(ngx.ERR, "failed to create timer: ", err)
		return
	end
end
